package com.quartz.mq.infra.rabbitmq;

import com.quartz.mq.business.enums.ProcessStatusEnum;
import com.quartz.mq.business.service.RabbitQuartzService;
import com.quartz.mq.config.RabbitConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * <h3>partitionjob</h3>
 * <p>RabbitSender</p>
 *
 * @author : hduong
 * @version : 1.0
 * @date : 2019-12-19 20:37
 **/
@Component
public class RabbitSender {


    private final Logger logger = LoggerFactory.getLogger(RabbitSender.class);

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitQuartzService rabbitQuartzService;

    public void send(Long orderId) {
        logger.info("Start sending");

        rabbitQuartzService.changeOrderProcessStatus(orderId, ProcessStatusEnum.PROCESSING);

        // 此处发送的消息已经是持久化的，RabbitTemplate中send方法中的message里面的messageProperties默认的DeliveryMode是MessageDeliveryMode.PERSISTENT
        this.rabbitTemplate.convertAndSend(RabbitConfig.ORDER_EXCHANGE, RabbitConfig.ORDER_ROUTING_KEY,
                orderId.toString());

        try {
            Thread.sleep(20000);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        logger.info("End sending \n");
    }
}
